package org.dromara.project.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.project.domain.vo.ProContractInfoVo;
import org.springframework.stereotype.Service;
import org.dromara.project.domain.bo.ProContractBo;
import org.dromara.project.domain.vo.ProContractVo;
import org.dromara.project.domain.ProContract;
import org.dromara.project.mapper.ProContractMapper;
import org.dromara.project.service.IProContractService;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 合同信息Service业务层处理
 *
 * @author LUSH
 * @date 2025-05-27
 */
@RequiredArgsConstructor
@Service
public class ProContractServiceImpl implements IProContractService {

    private final ProContractMapper baseMapper;

    /**
     * 查询合同信息
     *
     * @param id 主键
     * @return 合同信息
     */
    @Override
    public ProContractVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询合同信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 合同信息分页列表
     */
    @Override
    public TableDataInfo<ProContractVo> queryPageList(ProContractBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProContract> lqw = buildQueryWrapper(bo);
        Page<ProContractVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的合同信息列表
     *
     * @param bo 查询条件
     * @return 合同信息列表
     */
    @Override
    public List<ProContractVo> queryList(ProContractBo bo) {
        LambdaQueryWrapper<ProContract> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProContract> buildQueryWrapper(ProContractBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProContract> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ProContract::getId);
        lqw.like(StringUtils.isNotBlank(bo.getContractName()), ProContract::getContractName, bo.getContractName());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNumber()), ProContract::getContractNumber, bo.getContractNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerUnit()), ProContract::getOwnerUnit, bo.getOwnerUnit());
        lqw.eq(bo.getProjectRegion() != null, ProContract::getProjectRegion, bo.getProjectRegion());
        lqw.eq(bo.getPresidentId() != null, ProContract::getPresidentId, bo.getPresidentId());
        lqw.eq(bo.getConSignTime() != null, ProContract::getConSignTime, bo.getConSignTime());
        return lqw;
    }

    /**
     * 新增合同信息
     *
     * @param bo 合同信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ProContractBo bo) {
        ProContract add = MapstructUtils.convert(bo, ProContract.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改合同信息
     *
     * @param bo 合同信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ProContractBo bo) {
        ProContract update = MapstructUtils.convert(bo, ProContract.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProContract entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除合同信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public ProContractInfoVo queryInfoById(Long id) {
        ProContract entity = baseMapper.selectById(id);
        ProContractInfoVo vo = MapstructUtils.convert(entity, ProContractInfoVo.class);
        // projectRegionName等扩展字段可在此补充
        return vo;
    }
}
